package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.core.exception.Assert;
import com.atguigu.srb.core.exception.BusinessException;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.result.R;
import com.atguigu.srb.core.result.ResponseEnum;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级管理")
public class AdminIntegralGradeController {
    @Autowired
    private IntegralGradeService integralGradeService;

    @GetMapping("/list")
    @ApiOperation(value = "查询积分等级列表")
    public R listAll() {
        List<IntegralGrade> gradeList = integralGradeService.list();

        return R.ok().data("list", gradeList);
    }



    @GetMapping("/get/{id}")
    @ApiOperation(value = "指定id进行查询积分等级")
    public R getById(@PathVariable("id")
                     @ApiParam(value = "主键id", required = true, example = "1") Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);

        //int i = 10 / 0;

        if (integralGrade != null) {
            return R.ok().data("record", integralGrade);
        } else {
            return R.error().message("数据不存在啊!");
        }
    }


    @PostMapping("/save")
    @ApiOperation(value = "保存积分等级")
    public R save(@RequestBody
                        @ApiParam(value = "积分等级对象")
                                IntegralGrade integralGrade) throws BusinessException {
        /*if (integralGrade.getBorrowAmount()==null){
            throw new BusinessException(ResponseEnum.BAD_SQL_ERROR);
        }*/
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        integralGradeService.save(integralGrade);
        return R.ok().message("保存成功!");
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "指定id进行删除积分等级")
    public R removeById(@PathVariable("id")
                        @ApiParam(value = "主键id", required = true, example = "1")
                                Long id) {
        Boolean res = integralGradeService.removeById(id);
        if (res) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "指定id修改积分等级")
    @PutMapping("/update")
    public R updateById(@RequestBody
                        @ApiParam(value = "积分等级对象", required = true)
                                IntegralGrade integralGrade) {
        boolean res = integralGradeService.updateById(integralGrade);
        if (res) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }

}
